# Contributor: Ed Robinson <ed@reevoo.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Ed Robinson <ed@reevoo.com>
pkgname=fontforge
pkgver=20230101
pkgrel=5
pkgdesc="Free (libre) font editor"
url="https://fontforge.org/"
arch="all !s390x"
license="GPL-3.0-or-later"
makedepends="
	cmake
	giflib-dev
	gnu-libiconv-dev
	gtk+3.0-dev
	libspiro-dev
	libxml2-dev
	pango-dev
	potrace
	py3-setuptools
	python3-dev
	readline-dev
	samurai
	tiff-dev
	woff2-dev
	"
subpackages="
	$pkgname-dev
	$pkgname-libs
	$pkgname-gui
	$pkgname-doc
	$pkgname-lang
	py3-$pkgname:_py3
	"
install="$pkgname.post-install $pkgname.post-upgrade"
source="https://github.com/fontforge/fontforge/releases/download/$pkgver/fontforge-$pkgver.tar.xz
	fix-select.patch
	desktop-gui.patch
	gettext-0.22.patch
	"

case "$CARCH" in
x86)
	# timeout after 2 hours
	options="$options !check"
	;;
esac

build() {
	export CFLAGS="$CFLAGS -flto=auto -I/usr/include/gnu-libiconv"
	export CXXFLAGS="$CXXFLAGS -flto=auto -I/usr/include/gnu-libiconv"
	_build build-nogui -DENABLE_GUI=OFF -DBUILD_TESTING=OFF
	_build build -DENABLE_GUI=ON -DBUILD_TESTING="$(want_check && echo ON || echo OFF)"
}

_build() {
	local builddir=$1; shift

	if [ "$CBUILD" != "$CHOST" ]; then
		local crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B "$builddir" -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_SKIP_INSTALL_RPATH=ON \
		-DENABLE_X11=OFF \
		-DENABLE_PYTHON_SCRIPTING=ON \
		-DENABLE_PYTHON_EXTENSION=ON \
		-DENABLE_LIBSPIRO=ON \
		-DENABLE_LIBGIF=ON \
		-DENABLE_LIBJPEG=ON \
		-DENABLE_LIBPNG=ON \
		-DENABLE_LIBREADLINE=ON \
		-DENABLE_LIBTIFF=ON \
		-DENABLE_WOFF2=ON \
		-DENABLE_DOCS=OFF \
		$crossopts \
		"$@"
	cmake --build "$builddir"
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	pkgdesc="$pkgdesc (CLI tools)"

	DESTDIR="$pkgdir" cmake --install build
	DESTDIR=".dest-nogui" cmake --install build-nogui

	mv "$pkgdir"/usr/bin/fontforge "$pkgdir"/usr/bin/fontforge-gui
	mv .dest-nogui/usr/bin/fontforge "$pkgdir"/usr/bin/
}

gui() {
	pkgdesc="$pkgdesc (GUI)"
	# NOTE: py3-setuptools is needed for the plugins discovery; FontForge
	#  complains when it's not available.
	depends="$pkgname=$pkgver-r$pkgrel py3-setuptools"

	amove usr/bin/fontforge-gui
	amove usr/share/applications
	amove usr/share/icons
	amove usr/share/metainfo
}

_py3() {
	pkgdesc="Python 3 bindings for $pkgname"

	amove usr/lib/python3.*
	amove usr/share/fontforge/python
}

sha512sums="
7684a2824d5199e230d2e0a654c2119cef422a57b64e6dd75afde6ba623abed6581f596fd5b7509e03f3b3ad3cf69629aafd4c85aeda64fa91a9e1ab88c9b418  fontforge-20230101.tar.xz
8cb3fb00fa537eab2d4b194916343a0e2aab85c2415a1944c0d0e648d62a7748d5d4465fb99cb92385b749e73abfcaa429f4c9d2bb4c2edea8febeb227add93a  fix-select.patch
cd45de343f3776c312df8f1e3cf10be004a5664981911023f9d9e2dbb05fab0adc02bbf9aaff4bdd716355a58cf4edcab8b5a806f76997c1ddc426239d94751a  desktop-gui.patch
9c59f24c142373b780992589fba6845c975dd36d376dc069adec3e63e09e9efb757be786d82c0c86cb331965aee1afdb872b4e16768daab075097256eb1002a4  gettext-0.22.patch
"
